﻿using System.Collections.Generic;
using System.Linq;
using IapService.Entity;
using NHibernate.Criterion;

namespace IapService.Data
{
    public class UserData : Data<User>
    {
        public User Get(string userName, string userPass)
        {
            User item;
            using (var session = Session)
            {
                item = session.CreateCriteria<User>()
                    .Add(Restrictions.And(
                        Restrictions.Eq("Name", userName), Restrictions.Eq("Passmot", userPass)
                        ))
                    .UniqueResult<User>();
                item.Passmot = string.Empty;
            }
            return item;
        }

        public User Get(int id)
        {
            User user;
            using (var session = Session)
            {
                user = session.Get<User>(id);
                user.Passmot = string.Empty;
            }
            return user;
        }

        public new List<User> Get()
        {
            List<User> users;
            using (var session = Session)
            {
                users = session.CreateCriteria<User>()
                    .List<User>()
                    .ToList();
                users.ForEach(s => s.Passmot = string.Empty);
            }
            return users;
        }
    }
}